// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts: [], //数据 
    iscart: false,
    hidden: null,
    isAllSelect: false,
    totalMoney: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.cartShow();
  },
  cartShow: function(){
    var shopCar = wx.getStorageSync('shopCar') || [];
    console.log(shopCar)
    if(shopCar.length >=1){
      this.setData({
        carts: shopCar,
        iscart: true,
        hidden: false
      })
      console.log(this.data.carts)
    }else{
      this.setData({
        iscart: false,
        hidden: true
      })
    }


  },

  delCount: function(e){
    console.log(e);
    var index = e.target.dataset.index;
    var c_count = this.data.carts[index].c_count;
    
    if(c_count > 1){
      this.data.carts[index].c_count--;
      
    }else{
      wx.showToast({
        title: '不能再减啦',
      })
    }
    this.setData({
      carts: this.data.carts
    })
    console.log(this.data.carts);
  },
  addCount: function(e){
    console.log(e);
    var index = e.target.dataset.index;
    var c_count = this.data.carts[index].c_count;
    var count = this.data.carts[index].count;
    console.log(c_count);
    if (c_count < count){
      this.data.carts[index].c_count++;
     
    }else{
      wx.showToast({
        title: '不能再加啦',
      })
    }
    this.setData({
      carts: this.data.carts
    })
    console.log(this.data.carts)
  },
  // 总价格计算
  priceCount: function(){
    for(var i = 0; i< this.data.carts.length;i++){
      
    }
  },

  switchSelect: function (e) {
    console.log(e)
    var allPrice = 0;
    var index = e.target.dataset.index;
    this.data.carts[index].isSelect = !this.data.carts[index].isSelect;
    if (this.data.carts[index].isSelect){
      this.data.totalMoney += this.data.carts[index].c_count * this.data.carts[index].price
    }else{
      this.data.totalMoney -= this.data.carts[index].c_count * this.data.carts[index].price
    }
    //判断是否全选
    for(var i =0 ;i < this.data.carts.length; i++){
      allPrice += this.data.carts[i].price * this.data.carts[i].c_count
    }
    if (allPrice == this.data.totalMoney){
      this.data.isAllSelect = true;
    }else{
      this.data.isAllSelect = false;
    }

    this.setData({
      carts: this.data.carts,
      totalMoney: this.data.totalMoney,
      isAllSelect: this.data.isAllSelect,
    })
  },

  allSelect: function(e){
    if(!this.data.isAllSelect){
      for(var i = 0; i<this.data.carts.length; i++){
        this.data.carts[i].isSelect = true;
        this.data.totalMoney += this.data.carts[i].price * this.data.carts[i].c_count
      }
    }else{
      for (i = 0; i < this.data.carts.length; i++) {
        this.data.carts[i].isSelect = false;
      }
      this.data.totalMoney = 0;
    }
    this.setData({
      carts: this.data.carts,
      isAllSelect: !this.data.isAllSelect,
      totalMoney: this.data.totalMoney,
    })
  },
  delGoods: function (e){
    console.log(e)
    var index = e.target.dataset.index;
    this.data.carts.splice(index,1);
    if(this.data.carts.length > 0){
      this.setData({
        carts: this.data.carts
      })
      wx.setStorageSync('shopCar', this.data.carts);
      this.priceCount();
    }else{
      this.setData({
        cart: this.data.carts,
        iscart: false,
        hidden: true,
      })
      wx.setStorageSync('shopCar', []);
    }
  },
  toBuy: function(){
    wx.showLoading({
      title: '正在下单...',
    })
  }

})